package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.enums.ActivityViewTypeEnum;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.ActivityService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.ActivityView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Classname ActivityController
 * @Description TODO
 * @Date 2020/11/8 18:50
 * @Author by maixy
 */
@RestController
@RequestMapping("/v1/activity")
public class ActivityController extends ApiController {

    @Resource
    private ActivityService activityService;

    /**
     * 我创建的活动列表
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse getMyActivityList(ApiRequest request) {
        String activitySearch = request.getDataParamAsString("activitySearch", "");
        Integer activityType = request.getDataParamAsInt("activityType");
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Page page = Page.validation(request.getPage());
        PageData<ActivityView> pageData = activityService.getMyActivityList(teamId, userId, activitySearch, activityType, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

    /**
     * 查找所有活动列表
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse newActivityList(ApiRequest request) {
        Page page = Page.validation(request.getPage());
        Integer activityType = request.getDataParamAsInt("activityType");
        PageData<ActivityView> pageData = activityService.activityList(ActivityViewTypeEnum.NEW.getId(), activityType, page);
        return ApiResponse.ok().addValueToData("pageData", pageData);
    }

    /**
     * 查找所有活动列表
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse hotActivityList(ApiRequest request) {
        Page page = Page.validation(request.getPage());
        Integer activityType = request.getDataParamAsInt("activityType");
        PageData<ActivityView> pageData = activityService.activityList(ActivityViewTypeEnum.HOT.getId(), activityType, page);
        return ApiResponse.ok().addValueToData("pageData", pageData);
    }

    /**
     * 我加入的活动列表
     *
     * @param request --
     * @return --
     */
    @Privilege
    public ApiResponse getJoinActivityList(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        Page page = Page.validation(request.getPage());
        PageData<ActivityView> pageData = activityService.getJoinActivityList(teamId, userId, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

    /**
     * 活动详情页
     * @param request --activityId 活动id
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse detail(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        ActivityView activityView = activityService.activityDetail(activityId, teamId, userId);
        return ApiResponse.ok().addValueToData("activityView", activityView);
    }

    /**
     * 我创建的活动列表
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse discoverActivityList(ApiRequest request) throws IOException {
        String activitySearch = request.getDataParamAsString("activitySearch", "");
        Integer activityType = request.getDataParamAsInt("activityType", 0);
        Page page = Page.validation(request.getPage());
        PageData<ActivityView> pageData = activityService.discoverActivityList(activitySearch, activityType, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }


    /**
     * 创建活动
     * @param request -- 活动名称
     *                -- 活动logo
     *                -- 活动描述
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse createActivity(ApiRequest request){
        String activityName = request.getDataParamAsString("activityName");
        Integer activityType = request.getDataParamAsInt("activityType");
        String activityLogoUrl = request.getDataParamAsString("activityLogoUrl");
        String activityDesc = request.getDataParamAsString("activityDesc");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        if (StringUtils.isBlank(activityName) || null == activityType || StringUtils.isBlank(activityLogoUrl) || StringUtils.isBlank(activityDesc)) {
            throw new MissParamException();
        }
        activityService.createActivity(activityName, activityType, activityLogoUrl, activityDesc, userId, teamId);
        return ApiResponse.ok();
    }

    /**
     * 编辑课程
     * @param request -- activityId 课程id
     *                -- activityName 课程名称
     *                -- activityLogoId 课程封面图片id
     *                -- activityDesc 课程描述
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse updateActivity(ApiRequest request){
        Long activityId = request.getDataParamAsLong("activityId");
        String activityName = request.getDataParamAsString("activityName");
        Integer activityType = request.getDataParamAsInt("activityType");
        String activityLogoUrl = request.getDataParamAsString("activityLogoUrl");
        String activityDesc = request.getDataParamAsString("activityDesc");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        if (StringUtils.isBlank(activityName) || null == activityType || StringUtils.isBlank(activityDesc) || null == activityId) {
            throw new MissParamException();
        }
        activityService.updateActivity(activityId, activityType, activityName, activityLogoUrl, activityDesc, userId, teamId);
        return ApiResponse.ok();
    }

    /**
     * 删除课程
     * @param request -- activityId 课程id
     * @return --
     */
    @Privilege
    public ApiResponse deleteActivity(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());

        if (null == activityId) {
            throw new TipException("参数缺失");
        }
        activityService.deleteActivity(teamId, userId, activityId);
        return ApiResponse.ok();
    }

    @Privilege
    public ApiResponse getExportActivity(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        return ApiResponse.ok().addValueToData("downloadUrl", activityService.getExportActivity(userId, teamId));
    }

}
